package com.canyinghao.cananimation;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;

/**
 *
 */
public final class CanAnimation {

    /**
     * 属性动画永久循环
     *
     * @param animator
     * @return Animator
     */

    public static Animator valueAnimatorForever(Animator animator) {
        return valueAnimatorRepeat(-1, animator);
    }

    /**
     * 属性动画循环一定次数
     *
     * @param count
     * @param animator
     * @return Animator
     */

    public static Animator valueAnimatorRepeat(final int count, Animator animator) {
        if (count <= 1 && count != -1) {
            return animator;
        }

        if (animator instanceof AnimatorValue) {
            AnimatorValue valueAnimator = (AnimatorValue) animator;

            valueAnimator.setLoopedCount(Math.max(-1, count - 1));

            return valueAnimator;
        }

        return animator;
    }

    /**
     * AnimatorSet循环播放
     *
     * @param count
     * @param animator
     * @return Animator
     */

    public static Animator animatorSetForever(final int count, final Animator animator) {
        return animatorSetRepeat(-1, animator);
    }

    /**
     * AnimatorSet循环播放一定次数
     *
     * @param count
     * @param animator
     * @return Animator
     */

    public static Animator animatorSetRepeat(final int count, final Animator animator) {

        if (!(animator instanceof AnimatorGroup)) {
            return animator;
        }

        ((AnimatorGroup) animator).setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                if (count == -1) {
                    animatorSetRepeat(count, animator);
                    animator.start();

                } else if (count > 0) {
                    int tempCount = count;
                    tempCount--;
                    if (tempCount > 0) {
                        animatorSetRepeat(tempCount, animator);
                        animator.start();
                    }
                }
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });

        return animator;
    }

    /**
     * 动画队列
     *
     * @param animators
     * @return Animator
     */

    public static Animator animationSequence(Animator... animators) {
        AnimatorGroup set = new AnimatorGroup();
        set.runSerially(animators);

        return set;
    }


    /**
     * 动画同时播放
     *
     * @param animators
     * @return Animator
     */

    public static Animator animationTogether(Animator... animators) {
        AnimatorGroup set = new AnimatorGroup();
        set.runParallel(animators);
        return set;
    }

    public static Animator run(final Runnable runnable) {

        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(0);

        animator.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                runnable.run();
            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {

            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
        return animator;
    }

    /**
     * 放大并逐渐消失
     * @param view
     * @return Animator
     */
    public static Animator animScale2Alpha(Component view) {
        AnimatorGroup set = new AnimatorGroup();
        AnimatorProperty sa = view.createAnimatorProperty();
        sa.scaleXFrom(0).scaleX(2).scaleYFrom(0).scaleY(2).setDuration(1000)
                .setLoopedCount(Integer.MAX_VALUE);
        AnimatorProperty aa = view.createAnimatorProperty();
        aa.alphaFrom(1).alpha(0).setDuration(1000);

        set.runSerially(sa, aa);
        return set;
    }

    /**
     * 变小然后变大，然后变回原始大小
     *
     * @param animators
     * @param view
     * @param end
     * @return Animator
     */
    public static Animator animSmall2Big(Component view, Animator animators,Animator end) {
        AnimatorGroup set = new AnimatorGroup();
        AnimatorProperty animatorProperty = view.createAnimatorProperty();
        animatorProperty.alphaFrom(0.5f).alpha(1).setDuration(200).setCurveType(Animator.CurveType.ACCELERATE);
        AnimatorProperty animatorProperty1 = view.createAnimatorProperty();
        animatorProperty1.scaleXFrom(0.6f).scaleX(1.2f).scaleYFrom(0.6f).scaleY(1.2f).setDuration(200).setCurveType(Animator.CurveType.ACCELERATE);
        AnimatorProperty animatorProperty2 = view.createAnimatorProperty();
        animatorProperty2.scaleXFrom(1.2f).scaleX(0.8f).scaleYFrom(1.2f).scaleY(0.8f).setDuration(300).setCurveType(Animator.CurveType.DECELERATE);
        AnimatorProperty animatorProperty3 = view.createAnimatorProperty();
        animatorProperty3.scaleXFrom(0.8f).scaleX(1f).scaleYFrom(0.8f).scaleY(1f).setDuration(200).setCurveType(Animator.CurveType.DECELERATE);

        set.runSerially(animatorProperty, animatorProperty1, animatorProperty2, animatorProperty3, animators,end);
        return set;
    }


}
